package com.ljf.framework.Async;

import com.ljf.framework.LjfManager;
import com.ljf.framework.context.LjfContext;
import com.ljf.framework.log.LjfLogManager;
import com.ljf.framework.log.LjfLogService;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 说明：
 *
 * @Auther: lijinfeng
 * @Date: 2024/8/1
 */
public class AsyncManager {

    static LjfLogService log = LjfLogManager.getLogger(AsyncManager.class);

    /**
     * 有返回值的异步任务，使用默认线程池
     * @param asyncEvent 异步事件
     * @param <T> 返回值类型
     * @return AsyncResult
     */
    public static  <T> AsyncResult<T> supplyAsync(AsyncEvent<T> asyncEvent){
        return LjfManager.getAsyncService().supplyAsync(asyncEvent,null);
    }
    /**
     * 无返回值的异步任务，使用默认线程池
     * @param asyncEvent 异步事件
     */
    public static void runAsync(AsyncEvent asyncEvent) {
        LjfManager.getAsyncService().runAsync(asyncEvent,null);
    }

    /**
     * 有返回值的异步任务，使用指定线程池
     * @param asyncEvent 异步事件
     * @param threadPoolExecutor 指定线程池
     * @param <T> 返回值类型
     * @return AsyncResult
     */
    public static  <T> AsyncResult<T> supplyAsync(AsyncEvent<T> asyncEvent, ThreadPoolExecutor threadPoolExecutor){
        return LjfManager.getAsyncService().supplyAsync(asyncEvent,threadPoolExecutor);
    }

    /**
     * 无返回值的异步任务，使用指定线程池
     * @param asyncEvent 异步事件
     * @param threadPoolExecutor 指定线程池
     */
    public static void runAsync(AsyncEvent asyncEvent, ThreadPoolExecutor threadPoolExecutor) {
        LjfManager.getAsyncService().runAsync(asyncEvent,threadPoolExecutor);
    }

    /**
     * 关闭线程池
     */
    public static void shutdown(){
        ThreadPoolConfiguration.getDbPoolExecutorService().shutdown();
        ThreadPoolConfiguration.getSystemPoolExecutorService().shutdown();
        log.info("ljf-framework ThreadPoolExecutor shutdown");
    }
}
